<?php

namespace admin;

class Fun
{
    //检查登录状态，未登录就跳转登录
    static function ckLogin(){
        global $power;
        $ucms_admin=self::getadminname();
        $ucms_psw=$_COOKIE['psw_'.cookiehash];
        $mjjinfo=Mod::getUser(["username"=>$ucms_admin]);
        if(!$mjjinfo || $ucms_psw!=md5($mjjinfo["psd"].Config(".siteHash"))){
            self::admin_error();
        }
        $power=json_decode($mjjinfo['power'],1);
        cSet("user",$mjjinfo);
        if(!power('b',1)) {
            self::admin_error('该账户已禁止登录');
        }
    }
    static function admin_error($info='登录信息有误,请重新登录') {
        R("/admin/login",1);
        exit();
    }
    static function getAdminName() {
        if (isset($_COOKIE['admin_'.cookiehash]) && !empty($_COOKIE['admin_'.cookiehash])){
            $username=trim($_COOKIE['admin_'.cookiehash]);
            if(!preg_match("/^[a-zA-Z0-9_]+$/",$username)){
                self::admin_error();
            }
            Return trim($username);
        }else {
            self::admin_error();
        }
    }
    static function setAdminName($value) {
        cookie('admin_'.cookiehash, $value, time()+604800);
    }
    static function setAdminPsd($value) {
        if(!empty($value)) {
            $value=md5($value.Config(".siteHash"));
        }
        cookie('psw_'.cookiehash,$value,time()+604800);
    }
    static function passwordMd5($password='') {
        Return md5(substr(md5($password),0,10).md5($password));
    }
}